package finalproject.model.player.pet;

import java.awt.Color;
import java.util.ArrayList;
import java.util.List;

import finalproject.model.player.Badge;
import finalproject.model.player.Energy;
import finalproject.model.player.Player;
import finalproject.model.player.fight.Ability;
import finalproject.model.player.fight.Fightable;
import finalproject.view.StringManager;

/**
 * The pet class
 * Represent a pet, its abilities, its name and its energy
 * @author Salah Dahmoul
 *
 */
public class Pet implements Fightable {

	/**
	 * The name of the pet
	 */
	private String name;
	/**
	 * The abilities of the pet
	 */
	private List<Ability> abilities;
	/**
	 * The badge of the pet
	 */
	private Badge badge;

	/**
	 * A constructor of pet
	 * The resulting pet won't have any abilities
	 * @param name the name of the pet
	 */
	public Pet(String name)
	{
		this.name = name;
		this.abilities = new ArrayList<Ability>();
		this.badge = new Badge();
	}
	
	/**
	 * A constructor of pet
	 * @param name the name of the pet
	 * @param a the list of the abilites of the pet
	 */
	public Pet(String name, ArrayList<Ability> a)
	{
		this.name = name;
		this.abilities = a;
		this.badge = new Badge();
	}

	public String getName()
	{
		return name;
	}

	public void setName(String name)
	{
		this.name = name;
	}
	public Badge getBadge() {
		return badge;
	}
	
	public void setBadge(Badge badge) {
		this.badge = badge;
	}
	@Override
	public List<Ability> getAbilities()
	{
		return abilities;
	}
	
	
	/* (non-Javadoc)
	 * @see finalproject.model.player.fight.Fightable#useAbilities(java.lang.String, finalproject.model.player.Player)
	 */
	@Override
	public boolean useAbilities(String ability, Player p1){
		
		if(this.getBadge().getEnergie().getEnergy() <= 0)
		{
			StringManager.getInstance().sendToDisplayer( Color.gray, this.getName() + " " + StringManager.getString("petnotenoughenergy") + "\n");
			return true;
		}
				
		Pet p = p1.getPet();
		for(Ability a : abilities){
			if (ability.equals(a.getName()))
			{
				if((p.getBadge().getEnergie().getEnergy() - a.getStrength()) >= 0)
				{
					p.getBadge().modifierEnergie(-a.getStrength());
					StringManager.getInstance().sendToDisplayer(Color.gray, this.getName()+ " " + StringManager.getString("attack")
							+ " " + a.getName() + " " + StringManager.getString("andremovedpetenemi") 
							+ " " + a.getStrength() + " " + StringManager.getString("pointenery") + "\n");
				}
				else{
					int strength = a.getStrength();
					while (p.getBadge().getEnergie().getEnergy() > 0)
					{
						p.getBadge().modifierEnergie(-1);
						strength--;
					}
					p1.getBadge().modifierEnergie(-strength);
					
					if(strength != a.getStrength())
					{
						
						StringManager.getInstance().sendToDisplayer(Color.gray, this.getName()+ " " + StringManager.getString("attack")
							+ " " + a.getName() + " " + StringManager.getString("andremovedpetenemi")
							+ " " + (a.getStrength()-strength) + " " + StringManager.getString("pointenery") 
							+" "+ StringManager.getString("alsoremovedPlayerenemi")
							+ " " + strength + " " + StringManager.getString("pointenery") + "\n"); 
					}else
					{
						StringManager.getInstance().sendToDisplayer(Color.gray, this.getName()+ " " + StringManager.getString("attack")
								+ " " + a.getName() + " " + StringManager.getString("andremovedPlayerenemi")
								+ " " + strength + " " + StringManager.getString("pointenery") + "\n");
					}
						
					
					
				}
					
				return true;
			}
		}
		return false;
	}
	
	/**
	 * Display the information of pet
	 *  
	 * name	energy
	 * list of abilities 
	 */
	
	public String toString(){
		String listAbility = "";
		
		for(int i=0;i<abilities.size();i++)
			listAbility = listAbility + abilities.get(i) + "\n";
		
		return name + "\t" + badge.getEnergie() + "\n" + listAbility + "\n";
	}

	public Energy getEnergy() {
		// TODO Auto-generated method stub
		return badge.getEnergie();
	}
}
